package cn.kinoko.service.common.util;

import cn.kinoko.common.utils.SpringHelper;
import cn.kinoko.config.minio.MinioProperties;

import java.util.function.BiConsumer;
import java.util.function.Function;

/**
 * @author kinoko
 */
public class ThumbPathHelper {

    private static final MinioProperties MINIO_PROPERTIES = SpringHelper.getBean(MinioProperties.class);

    /**
     * 将原始路径转换为缩略图路径
     *
     * @param target 目标
     * @param origin 原始路径获取函数
     * @param thumb  缩略图路径赋值函数
     * @param <T>    目标类型
     */
    public static <T> void toThumbPath(T target, Function<T, String> origin, BiConsumer<T, String> thumb) {
        String originPath = origin.apply(target);
        if (originPath != null) {
            String thumbPath = originPath
                    .replace(MINIO_PROPERTIES.getFilePathPrefix(), MINIO_PROPERTIES.getThumbPathPrefix());
            thumb.accept(target, thumbPath);
        }
    }

}
